﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;
using devtm.Configuration;

namespace devtm.Editor.Configuration.Merge
{
    /// <summary>
    /// Interaction logic for ChangeFrm.xaml
    /// </summary>
    public partial class ChangeFrm : Window
    {
        public ChangeFrm()
        {
            InitializeComponent();
        }

        private void SectionAdd(object sender, RoutedEventArgs e)
        {

            DiffItem i = (DiffItem)(sender as Button).DataContext;
            ConfigurationSection s1 = i.Right.Section;

            string name = s1.SectionInformation.Name;

            bool isPresent = ContainsKey(name);
            ConfigurationSection s2 = null;

            if (!isPresent)
            {
                s2 = (ConfigurationSection)Activator.CreateInstance(s1.GetType());
                s2.SectionInformation.SetRawXml(s1.SectionInformation.GetRawXml());
                Datas.Configuration.Sections.Add(name, s2);
            }
            else
            {
                ConfigurationSection ii = Datas.Configuration.Sections[name];
            }

           

            ElementSection items = (UCConfigEdit.ConfigEdit.TreeRoot.Items[0] as ElementSection);
            IElementSection section = items.AddConfigurationElement(s2, Name);
            section.ContextTitle.Inherited = System.Windows.Visibility.Hidden;
            section.CanDel = true;

            int index = (section.ParentItem as ElementSection).Items.IndexOf(section);
            //var configSection = Da =tas.Configuration.GetSection(makey);
            Command.CommandHelpers.HasAddedSection(index, section);

            DiffListe source = (DiffListe)this.DataContext;

            source.Remove(i);

            if (source.Count == 0)
                this.Close();

        }

        private bool ContainsKey(string name)
        {

            foreach (string item in Datas.Configuration.Sections.Keys)
                if (name == item)
                    return true;

            return false;

        }

        private void Skip(object sender, RoutedEventArgs e)
        {
            DiffItem i = (DiffItem)(sender as Button).DataContext;

            DiffListe source = (DiffListe)this.DataContext;

            source.Remove(i);

            if (source.Count == 0)
                this.Close();

        }

        private void SectionRemove(object sender, RoutedEventArgs e)
        {
            DiffItem i = (DiffItem)(sender as Button).DataContext;

            var o = i.Left.ElementSection;

            UCConfigEdit.ConfigEdit.DelSection(o);

            DiffListe source = (DiffListe)this.DataContext;

            source.Remove(i);
             
            if (source.Count == 0)
                this.Close();

        }

        private void SectionGet(object sender, RoutedEventArgs e)
        {

            if (Configuration != null)
            {
                SectionGet2(sender as Button);
                return;
            }


            Button button = sender as Button;
            DiffItem i = (DiffItem)button.DataContext;
            Diff o = (Diff)button.Tag;


            if (o.Side == Enumside.Right)
                UCConfigEdit.ConfigEdit.ReplaceSection(i.Left.ElementSection, o.Text, true);
            

            DiffListe source = (DiffListe)this.DataContext;
            source.Remove(i);
            if (source.Count == 0)
                this.Close();
        }

        private void SectionGet2(Button button)
        {

            DiffItem i = (DiffItem)button.DataContext;
            Diff o = (Diff)button.Tag;
            if (o.Side == Enumside.Right)
                return;

            i.Right.Section.SectionInformation.SetRawXml(i.Left.Text);

            DiffListe source = (DiffListe)this.DataContext;
            source.Remove(i);
            if (source.Count == 0)
                this.Close();

        }
        public System.Configuration.Configuration Configuration { get; set; }

    }
}
